package com.example.demo.Utils;

import android.os.Bundle;
import android.view.LayoutInflater;

import androidx.viewbinding.ViewBinding;

import com.example.demo.BaseActivity;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;

public abstract class BaseBindingActivity<T extends ViewBinding> extends BaseActivity {

    protected T binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 使用反射自动获取 Binding 的 inflate 方法
        Class<T> bindingClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        try {
            Method inflateMethod = bindingClass.getMethod("inflate", LayoutInflater.class);
            binding = (T) inflateMethod.invoke(null, getLayoutInflater());
        } catch (Exception e) {
            throw new RuntimeException("Failed to inflate binding", e);
        }

        // 将 binding.root 嵌套进 BaseActivity 的 container 中
        setContentView(binding.getRoot());
    }

    @Override
    protected void onDestroy() {
        binding = null;
        super.onDestroy();
    }
}